//
//  UUInputFunctionView.m
//  UUChatDemoForTextVoicePicture
//
//  Created by shake on 14-8-27.
//  Copyright (c) 2014年 uyiuyao. All rights reserved.
//

#import "UUInputFunctionView.h"
#import "UUProgressHUD.h"
#import <AVFoundation/AVFoundation.h>
#import "UUChatCategory.h"

@interface UUInputFunctionView () <UITextViewDelegate, AVAudioRecorderDelegate>
{
    BOOL isbeginVoiceRecord;
    NSInteger playTime;
	NSString *_docmentFilePath;
}

@property (nonatomic, strong) NSTimer *playTimer;
@property (nonatomic, strong) UILabel *placeHold;

@property (nonatomic, strong) AVAudioRecorder *recorder;

@property (nonatomic, weak, readonly) UIViewController *superVC;

@end

@implementation UUInputFunctionView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
	
    if (self) {
		self.backgroundColor = [UIColor whiteColor];
		self.isAbleToSendTextMessage = NO;

        //发送消息
        self.btnSendMessage = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.btnSendMessage setTitle:@"Send" forState:UIControlStateNormal];
        [self.btnSendMessage setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        self.btnSendMessage.titleLabel.font = [UIFont systemFontOfSize:14];
        self.btnSendMessage.layer.borderColor = [[UIColor grayColor] CGColor];
        self.btnSendMessage.layer.borderWidth = 1;
        self.btnSendMessage.layer.cornerRadius = 4;
        self.btnSendMessage.layer.masksToBounds = YES;
        
        [self.btnSendMessage addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.btnSendMessage];
        
        //输入框
		self.textViewInput = [[UITextView alloc] init];
        self.textViewInput.layer.cornerRadius = 4;
        self.textViewInput.layer.masksToBounds = YES;
        self.textViewInput.delegate = self;
        self.textViewInput.font = [UIFont systemFontOfSize:18];
        self.textViewInput.layer.borderWidth = 1;
        self.textViewInput.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        [self addSubview:self.textViewInput];
        
        //分割线
        CALayer *line = [CALayer layer];
        line.frame = CGRectMake(0, 0, self.uu_width, 1);
        line.backgroundColor = [[UIColor grayColor] CGColor];
        [self.layer addSublayer:line];
        
        //添加通知
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewDidEndEditing:) name:UIKeyboardWillHideNotification object:nil];
        
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
    }
    return self;
}

- (void)layoutSubviews
{
	[super layoutSubviews];
	self.btnSendMessage.frame = CGRectMake(self.uu_width-44 - 8, 8, 44, 44);
	self.textViewInput.frame = CGRectMake(8, 8, self.uu_width-68, self.uu_height - 8 * 2);
	_placeHold.frame = CGRectMake(20, 0, 200, 30);
}

- (UIViewController *)superVC
{
	return [self uu_findNextResonderInClass:[UIViewController class]];
}

- (void)RemindDragExit:(UIButton *)button
{
    [UUProgressHUD changeSubTitle:@"Release to cancel"];
}

- (void)RemindDragEnter:(UIButton *)button
{
    [UUProgressHUD changeSubTitle:@"Slide up to cancel"];
}

//发送消息（文字图片）
- (void)sendMessage:(UIButton *)sender
{
    NSString *resultStr = [self.textViewInput.text stringByReplacingOccurrencesOfString:@"   " withString:@""];
    [self.delegate UUInputFunctionView:self sendMessage:resultStr];
}


#pragma mark - TextViewDelegate

- (void)textViewDidEndEditing:(UITextView *)textView
{
    _placeHold.hidden = self.textViewInput.text.length > 0;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
